package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.adapter.machine.ChildAreaAdapter;
import com.sgb.kjwl.view.ui.adapter.machine.ParentAreaAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class TwoAreaPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private LinearLayout bottomLayout;
    public ParentAreaAdapter parentAreaAdapter;
    private ChildAreaAdapter childAreaAdapter;
    private List<ParentAreaEntity> selectedEntities;
    private List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList;
    private String no = "";
    private OnCloseBack onCloseBack;

    public TwoAreaPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_area_two;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bottomLayout = holder.getPop().findViewById(R.id.ll_next);
        okView = holder.getPop().findViewById(R.id.tv_ok);
        resetView = holder.getPop().findViewById(R.id.tv_next);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(no, selectedEntities, null);
                    Log.d("城市编号", no);
                }
            }
        });

        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomLayout.setVisibility(View.GONE);
                recyclerView.setAdapter(parentAreaAdapter);
                parentAreaAdapter.setDatas(areaEntityList);
            }
        });
        parentAreaAdapter = new ParentAreaAdapter(mContext, null);
        parentAreaAdapter.setItemSelect(true);
        parentAreaAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(parentAreaAdapter);
        parentAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity, int position) {
                if(TextUtils.isEmpty(listParentAreaEntity.getNo())){//no=""代表不限
                    no = "";
                    selectedEntities =new ArrayList<>();
                    okView.callOnClick();
                    return;
                }
                if (!no.equals(listParentAreaEntity.getNo())) {
                    childAreaAdapter.resetCheckDatas();
                }
                no = listParentAreaEntity.getNo();
                bottomLayout.setVisibility(View.VISIBLE);
                recyclerView.setAdapter(childAreaAdapter);
                childAreaAdapter.setDatas(listParentAreaEntity.getChildren());
            }
        });

        childAreaAdapter = new ChildAreaAdapter(mContext, null);
        childAreaAdapter.setMultiSelect(true);
        childAreaAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<ParentAreaEntity>() {
            @Override
            public void onItemMultiClick(List<ParentAreaEntity> parentAreaEntities, int position) {
                selectedEntities = parentAreaEntities;

            }
        });
    }

    public void setData(List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList, boolean isChange) {
        this.areaEntityList = areaEntityList;
        parentAreaAdapter.setDatas(areaEntityList);
        if (isChange) {
            bottomLayout.setVisibility(View.GONE);
            parentAreaAdapter.resetCheckDatas();
            recyclerView.setAdapter(parentAreaAdapter);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCloseBack != null) {
            onCloseBack.dismiss();
        }
    }

    public interface OnCloseBack {
        void dismiss();
    }

    public void setOnCloseBack(OnCloseBack onCloseBack) {
        this.onCloseBack = onCloseBack;
    }
}
